package com.xn.app.muying.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;

import com.lzy.okhttputils.OkHttpUtils;
import com.xn.app.muying.R;
import com.xn.app.muying.adpter.FarMonitorListAdapter;
import com.xn.app.muying.bean.FarMonitorListBean;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.MyAlertDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Request;

/**
 * Created by Administrator on 2016/11/25.
 */
public class FarMonitorListActivity extends BaseActivity {
    private AlertDialog zhuce_dialog;
    //数据库
    private DBManager db = null;
    private FarMonitorListAdapter farMonitorListAdapter;
    private ArrayList<FarMonitorListBean> list = null;
    public static boolean isDownload;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_farmonitorlist);
        db = new DBManager(FarMonitorListActivity.this);
        ListView lv = (ListView) findViewById(R.id.far_monitorlist_listview);
        findViewById(R.id.far_monitorlist_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        farMonitorListAdapter = new FarMonitorListAdapter(FarMonitorListActivity.this);
        lv.setAdapter(farMonitorListAdapter);
        getListData();
    }

    private void getListData() {
        zhuce_dialog = new MyAlertDialog().ShowDialog(FarMonitorListActivity.this, R.layout.dialog_loading);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Request request = new Request.Builder().url(APIMethod.FarMonitorList(db.select("antenatalCareHospitalId"))).addHeader("Authorization", db.select("token_type") + " " + db.select("access_token")).get().build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();
                    int code = response.code();
                    String bodys = response.body().string();
                    if (code == 200 || code == 204) {
                        onJsonData(bodys);
                    } else {
                        loginHandler.sendEmptyMessage(2);
                    }
                    zhuce_dialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 获取线程返回的数据，更新UI组件
     */
    @SuppressLint("HandlerLeak")
    Handler loginHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (zhuce_dialog != null) {
                zhuce_dialog.dismiss();
            }
            switch (msg.what) {
                case 1:
                    farMonitorListAdapter.getData(list);
                    break;
                case 2:
                    break;
                default:
                    break;
            }
        }
    };

    private void onJsonData(String bodys) {
        try {
            JSONObject jsonObject = new JSONObject(bodys);
            JSONArray items = jsonObject.getJSONArray("items");
            list = new ArrayList<>();
            for (int i = 0; i < items.length(); i++) {
                JSONObject object = items.getJSONObject(i);
                String remoteMonitorReportStatusDescription = object.optString("remoteMonitorReportStatusDescription");
                String companyName = object.optString("companyName");
                String serviceTypeDescription = object.optString("serviceTypeDescription");
                String reportGenerateTime = object.optString("reportGenerateTime");
                String reportUrl = object.optString("reportUrl");
                FarMonitorListBean farMonitorListBean = new FarMonitorListBean();
                farMonitorListBean.setRemoteMonitorReportStatusDescription(remoteMonitorReportStatusDescription);
                farMonitorListBean.setCompanyName(companyName);
                farMonitorListBean.setServiceTypeDescription(serviceTypeDescription);
                farMonitorListBean.setReportGenerateTime(reportGenerateTime);
                farMonitorListBean.setReportUrl(reportUrl);
                list.add(farMonitorListBean);
            }
            loginHandler.sendEmptyMessage(1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //Activity销毁时，取消网络请求
        OkHttpUtils.getInstance().cancelTag(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

}
